package coml.jiyun.hp.day07;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import java.util.List;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements SensorEventListener, View.OnClickListener {

    private SensorManager manager;
    /**
     * x:
     */
    private TextView mTvX;
    /**
     * y:
     */
    private TextView mTvY;
    /**
     * z:
     */
    private TextView mTvZ;
    private Vibrator vibrator;
    private TextView mTv4;
    private TextView mTv5;
    private TextView mTv6;
    private TextView mTv7;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        //传感器管理器
        manager = (SensorManager) getSystemService(SENSOR_SERVICE);
        //震动管理器
        vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        //获取设备上所有的传感器
        List<Sensor> sensorList = manager.getSensorList(Sensor.TYPE_ALL);
        for (int i = 0; i < sensorList.size(); i++) {
            Log.d("TAG", "onCreate: " + sensorList.get(i).getName());
        }


    }

    //传感器数据变化时回调
    //所有的传感器数据都是这个回调方法拿到的
    @Override
    public void onSensorChanged(SensorEvent event) {
        //精度
//        event.accuracy
        //时间戳
//        event.timestamp
        //传感器的对象
//        event.sensor
        //传感器的数据
        int type = event.sensor.getType();
        if (type == Sensor.TYPE_ACCELEROMETER) {
            //加速度传感器
            float x = event.values[0];
            float y = event.values[1];
            float z = event.values[2];
            Log.d("TAG", "x " + x);
            mTvX.setText("x:" + x);
            mTvY.setText("y:" + y);
            mTvZ.setText("z:" + z);

            if (Math.abs(x) > 17 || Math.abs(y) > 17 && Math.abs(z) > 17) {
                //摇一摇
                shake();
            }
        } else if (type == Sensor.TYPE_ORIENTATION) {
            //绕Z轴转过的角度 0度或者360度时指向正北方
            float z = event.values[0];
            float x = event.values[1];
            float y = event.values[2];
            mTv4.setText("绕z轴转过:" + z);
            mTv5.setText("绕x轴转过:" + x);
            mTv6.setText("绕y轴转过:" + y);
        }else if (type == Sensor.TYPE_LIGHT){
            float x = event.values[0];
            mTv7.setText("光照强度:"+x);
        }
    }

    private void shake() {
        //1.音效
        MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.weichat_audio);
        mediaPlayer.start();
        //2.震动
        long[] patter = {1000, 1000, 2000, 3000};
        vibrator.vibrate(patter, -1);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //加速度传感器
        manager.registerListener(this, manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
        //方向传感器
        manager.registerListener(this, manager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_NORMAL);
        //光照传感器
        manager.registerListener(this, manager.getDefaultSensor(Sensor.TYPE_LIGHT), SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void onPause() {
        super.onPause();
        //注销
        manager.unregisterListener(this);
    }

    //传感器精度变化时回调
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

    private void initView() {
        mTvX = (TextView) findViewById(R.id.tv_x);
        mTvY = (TextView) findViewById(R.id.tv_y);
        mTvZ = (TextView) findViewById(R.id.tv_z);
        mTvX.setOnClickListener(this);
        mTvY.setOnClickListener(this);
        mTvZ.setOnClickListener(this);
        mTv4 = (TextView) findViewById(R.id.tv_4);
        mTv5 = (TextView) findViewById(R.id.tv_5);
        mTv6 = (TextView) findViewById(R.id.tv_6);
        mTv4.setOnClickListener(this);
        mTv5.setOnClickListener(this);
        mTv6.setOnClickListener(this);
        mTv7 = (TextView) findViewById(R.id.tv_7);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.tv_x:
                break;
            case R.id.tv_y:
                break;
            case R.id.tv_z:
                break;
            case R.id.tv_4:
                break;
            case R.id.tv_5:
                break;
            case R.id.tv_6:
                break;
        }
    }
}
